/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.shop.editor;

import idea.model.qnrlibrary.EqcUsers;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.PopupMenu;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.swing.Icon;
import javax.swing.JPopupMenu;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.netbeans.api.settings.ConvertAsProperties;
import org.netbeans.spi.actions.AbstractSavable;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.openide.NotifyDescriptor.Confirmation;
import org.openide.NotifyDescriptor.Message;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.UndoRedo;
import org.openide.util.ImageUtilities;
import org.openide.util.Lookup;
import org.openide.util.LookupEvent;
import org.openide.util.LookupListener;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;
import org.openide.windows.WindowManager;
import org.openide.util.lookup.InstanceContent;
import org.shop.viewer.UserViewerTopComponent;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(
        dtd = "-//org.shop.editor//UserEditor//EN",
        autostore = false)
@TopComponent.Description(
        preferredID = "UserEditorTopComponent",
        //iconBase="SET/PATH/TO/ICON/HERE", 
        persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(mode = "editor", openAtStartup = false)
@ActionID(category = "Window", id = "org.shop.editor.UserEditorTopComponent")
@ActionReferences(
        {
    @ActionReference(path = "Menu/Window", position = 1233),
    @ActionReference(path = "Menu/File/Open", position = 2100)
})
@TopComponent.OpenActionRegistration(
        displayName = "#CTL_UserEditorAction",
        preferredID = "UserEditorTopComponent")
@Messages({
    "CTL_UserEditorAction=UserEditor",
    "CTL_UserEditorTopComponent=User Editor Window",
    "HINT_UserEditorTopComponent=This is a User Editor window"
})
public final class UserEditorTopComponent extends TopComponent implements LookupListener {
    //private InstanceContent content;

    private UndoRedo.Manager manager = new UndoRedo.Manager();
    private Lookup.Result result = null;
    //private final InstanceContent content;
    private final InstanceContent instanceContent;
    private EqcUsers eqcusers;
    private boolean addnew = false;
    private JPopupMenu popup;
    private PopupMenu menuItem;
    final static String LOOKANDFEEL = "Metal";

    public UserEditorTopComponent() throws ClassNotFoundException, IllegalAccessException, InstantiationException, UnsupportedLookAndFeelException {

        UIManager.setLookAndFeel(
                UIManager.getCrossPlatformLookAndFeelClassName());

        initComponents();
        setName(Bundle.CTL_UserEditorTopComponent());
        setToolTipText(Bundle.HINT_UserEditorTopComponent());

        //ActionMap map = this.getActionMap();
        //map.put("delete", ExplorerUtils.actionDelete(em, true)); //NOI18N
        //associateLookup(ExplorerUtils.createLookup(em, map));


        instanceContent = new InstanceContent();
        uanameField.getDocument().addUndoableEditListener(manager);
        descriptionField.getDocument().addUndoableEditListener(manager);
        nameField.getDocument().addUndoableEditListener(manager);
        titleField.getDocument().addUndoableEditListener(manager);
        phonenumberField.getDocument().addUndoableEditListener(manager);
        faxnumberField.getDocument().addUndoableEditListener(manager);
        telexnumberField.getDocument().addUndoableEditListener(manager);
        mailingaddressField.getDocument().addUndoableEditListener(manager);
        emailaddressField.getDocument().addUndoableEditListener(manager);
        tradeareaField.getDocument().addUndoableEditListener(manager);
        datecreatedField.getDocument().addUndoableEditListener(manager);
        datemodifiedField.getDocument().addUndoableEditListener(manager);
        createdbyField.getDocument().addUndoableEditListener(manager);
        modifiedbyField.getDocument().addUndoableEditListener(manager);

        tradeareaField.setDocument(new LengthRestrictedDocument(8));
        /*
         this.addMouseListener(
         new MouseListener() {
         @Override
         public void mouseClicked(MouseEvent e) {
         if (e.getButton() == MouseEvent.BUTTON3) {
         mouseActionListener();
         }
         }

         @Override
         public void mousePressed(MouseEvent e) {
         }

         @Override
         public void mouseReleased(MouseEvent e) {
         }

         @Override
         public void mouseEntered(MouseEvent e) {
         }

         @Override
         public void mouseExited(MouseEvent e) {
         }
         });
         */
        //uanameField.addKeyListener(new KeyAdapter() {
        //Override
        //public void keyReleased(KeyEvent e) {
        //    modify();
        //}
        //});
        descriptionField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                modify();
            }
        });
        nameField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                modify();
            }
        });
        titleField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                modify();
            }
        });
        phonenumberField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                modify();
            }
        });
        faxnumberField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                modify();
            }
        });
        telexnumberField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                modify();
            }
        });
        mailingaddressField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                modify();
            }
        });
        emailaddressField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                modify();
            }
        });
        tradeareaField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                modify();
            }
        });
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {

        userDeleteActionListener1 = new org.shop.editor.UserDeleteActionListener();
        jLabel1 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        jLabel18 = new javax.swing.JLabel();
        uanameField = new javax.swing.JTextField();
        descriptionField = new javax.swing.JTextField();
        nameField = new javax.swing.JTextField();
        titleField = new javax.swing.JTextField();
        phonenumberField = new javax.swing.JTextField();
        faxnumberField = new javax.swing.JTextField();
        emailaddressField = new javax.swing.JTextField();
        datecreatedField = new javax.swing.JTextField();
        telexnumberField = new javax.swing.JTextField();
        mailingaddressField = new javax.swing.JTextField();
        tradeareaField = new javax.swing.JTextField();
        createdbyField = new javax.swing.JTextField();
        datemodifiedField = new javax.swing.JTextField();
        modifiedbyField = new javax.swing.JTextField();
        addUserButton = new javax.swing.JButton();
        deleteUserButton = new javax.swing.JButton();

        setBackground(new java.awt.Color(255, 255, 255));
        setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        setForeground(new java.awt.Color(192, 192, 192));
        setOpaque(true);

        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(UserEditorTopComponent.class, "UserEditorTopComponent.jLabel1.text")); // NOI18N

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(jLabel5, org.openide.util.NbBundle.getMessage(UserEditorTopComponent.class, "UserEditorTopComponent.jLabel5.text")); // NOI18N

        jLabel6.setBackground(new java.awt.Color(0, 0, 0));
        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(jLabel6, org.openide.util.NbBundle.getMessage(UserEditorTopComponent.class, "UserEditorTopComponent.jLabel6.text")); // NOI18N

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(jLabel7, org.openide.util.NbBundle.getMessage(UserEditorTopComponent.class, "UserEditorTopComponent.jLabel7.text")); // NOI18N

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(jLabel8, org.openide.util.NbBundle.getMessage(UserEditorTopComponent.class, "UserEditorTopComponent.jLabel8.text")); // NOI18N

        jLabel9.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(jLabel9, org.openide.util.NbBundle.getMessage(UserEditorTopComponent.class, "UserEditorTopComponent.jLabel9.text")); // NOI18N

        jLabel10.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(jLabel10, org.openide.util.NbBundle.getMessage(UserEditorTopComponent.class, "UserEditorTopComponent.jLabel10.text")); // NOI18N

        jLabel11.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(jLabel11, org.openide.util.NbBundle.getMessage(UserEditorTopComponent.class, "UserEditorTopComponent.jLabel11.text")); // NOI18N

        jLabel12.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(jLabel12, org.openide.util.NbBundle.getMessage(UserEditorTopComponent.class, "UserEditorTopComponent.jLabel12.text")); // NOI18N

        jLabel13.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(jLabel13, org.openide.util.NbBundle.getMessage(UserEditorTopComponent.class, "UserEditorTopComponent.jLabel13.text")); // NOI18N

        jLabel14.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(jLabel14, org.openide.util.NbBundle.getMessage(UserEditorTopComponent.class, "UserEditorTopComponent.jLabel14.text")); // NOI18N

        jLabel15.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(jLabel15, org.openide.util.NbBundle.getMessage(UserEditorTopComponent.class, "UserEditorTopComponent.jLabel15.text")); // NOI18N

        jLabel16.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(jLabel16, org.openide.util.NbBundle.getMessage(UserEditorTopComponent.class, "UserEditorTopComponent.jLabel16.text")); // NOI18N

        jLabel17.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(jLabel17, org.openide.util.NbBundle.getMessage(UserEditorTopComponent.class, "UserEditorTopComponent.jLabel17.text")); // NOI18N

        jLabel18.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(jLabel18, org.openide.util.NbBundle.getMessage(UserEditorTopComponent.class, "UserEditorTopComponent.jLabel18.text")); // NOI18N

        uanameField.setText(org.openide.util.NbBundle.getMessage(UserEditorTopComponent.class, "UserEditorTopComponent.uanameField.text")); // NOI18N
        uanameField.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                uanameFieldActionPerformed(evt);
            }
        });

        descriptionField.setText(org.openide.util.NbBundle.getMessage(UserEditorTopComponent.class, "UserEditorTopComponent.descriptionField.text")); // NOI18N
        descriptionField.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                descriptionFieldActionPerformed(evt);
            }
        });

        nameField.setText(org.openide.util.NbBundle.getMessage(UserEditorTopComponent.class, "UserEditorTopComponent.nameField.text")); // NOI18N

        titleField.setText(org.openide.util.NbBundle.getMessage(UserEditorTopComponent.class, "UserEditorTopComponent.titleField.text")); // NOI18N
        titleField.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                titleFieldActionPerformed(evt);
            }
        });

        phonenumberField.setText(org.openide.util.NbBundle.getMessage(UserEditorTopComponent.class, "UserEditorTopComponent.phonenumberField.text")); // NOI18N
        phonenumberField.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                phonenumberFieldActionPerformed(evt);
            }
        });

        faxnumberField.setText(org.openide.util.NbBundle.getMessage(UserEditorTopComponent.class, "UserEditorTopComponent.faxnumberField.text")); // NOI18N

        emailaddressField.setText(org.openide.util.NbBundle.getMessage(UserEditorTopComponent.class, "UserEditorTopComponent.emailaddressField.text")); // NOI18N
        emailaddressField.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                emailaddressFieldActionPerformed(evt);
            }
        });

        datecreatedField.setEditable(false);
        datecreatedField.setText(org.openide.util.NbBundle.getMessage(UserEditorTopComponent.class, "UserEditorTopComponent.datecreatedField.text")); // NOI18N
        datecreatedField.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                datecreatedFieldActionPerformed(evt);
            }
        });

        telexnumberField.setText(org.openide.util.NbBundle.getMessage(UserEditorTopComponent.class, "UserEditorTopComponent.telexnumberField.text")); // NOI18N
        telexnumberField.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                telexnumberFieldActionPerformed(evt);
            }
        });

        mailingaddressField.setText(org.openide.util.NbBundle.getMessage(UserEditorTopComponent.class, "UserEditorTopComponent.mailingaddressField.text")); // NOI18N
        mailingaddressField.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                mailingaddressFieldActionPerformed(evt);
            }
        });

        tradeareaField.setText(org.openide.util.NbBundle.getMessage(UserEditorTopComponent.class, "UserEditorTopComponent.tradeareaField.text")); // NOI18N
        tradeareaField.setMaximumSize(new java.awt.Dimension(8, 8));

        createdbyField.setEditable(false);
        createdbyField.setText(org.openide.util.NbBundle.getMessage(UserEditorTopComponent.class, "UserEditorTopComponent.createdbyField.text")); // NOI18N

        datemodifiedField.setEditable(false);
        datemodifiedField.setText(org.openide.util.NbBundle.getMessage(UserEditorTopComponent.class, "UserEditorTopComponent.datemodifiedField.text")); // NOI18N

        modifiedbyField.setEditable(false);
        modifiedbyField.setText(org.openide.util.NbBundle.getMessage(UserEditorTopComponent.class, "UserEditorTopComponent.modifiedbyField.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(addUserButton, org.openide.util.NbBundle.getMessage(UserEditorTopComponent.class, "UserEditorTopComponent.addUserButton.text")); // NOI18N
        addUserButton.setToolTipText(org.openide.util.NbBundle.getMessage(UserEditorTopComponent.class, "UserEditorTopComponent.addUserButton.toolTipText")); // NOI18N
        addUserButton.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                addUserButtonActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(deleteUserButton, org.openide.util.NbBundle.getMessage(UserEditorTopComponent.class, "UserEditorTopComponent.deleteUserButton.text")); // NOI18N
        deleteUserButton.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                deleteUserButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jLabel18, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel17, javax.swing.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE)
                        .addComponent(jLabel11, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel13, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel14, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel15, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel16, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(mailingaddressField, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(datecreatedField, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(uanameField, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(descriptionField, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(phonenumberField, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(faxnumberField, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(emailaddressField, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(titleField, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(createdbyField, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tradeareaField, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(29, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(telexnumberField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE)
                                    .addComponent(datemodifiedField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE))
                                .addComponent(modifiedbyField, javax.swing.GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(addUserButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(deleteUserButton)))
                        .addGap(0, 0, Short.MAX_VALUE))))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {createdbyField, datecreatedField, datemodifiedField, descriptionField, emailaddressField, faxnumberField, mailingaddressField, modifiedbyField, nameField, phonenumberField, telexnumberField, titleField, tradeareaField});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel10, jLabel11, jLabel12, jLabel13, jLabel14, jLabel15, jLabel16, jLabel17, jLabel18, jLabel5, jLabel6, jLabel7, jLabel8, jLabel9});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(4, 4, 4)
                        .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel13)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel14, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tradeareaField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel15, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel16)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel17)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel18))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(uanameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(descriptionField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(titleField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(phonenumberField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(faxnumberField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(53, 53, 53)
                                .addComponent(jLabel1)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(telexnumberField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(4, 4, 4)
                        .addComponent(mailingaddressField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(emailaddressField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(32, 32, 32)
                        .addComponent(datecreatedField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(createdbyField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(datemodifiedField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(modifiedbyField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addUserButton)
                    .addComponent(deleteUserButton))
                .addContainerGap(39, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {createdbyField, datecreatedField, datemodifiedField, descriptionField, emailaddressField, faxnumberField, jLabel10, jLabel11, jLabel12, jLabel13, jLabel14, jLabel15, jLabel16, jLabel17, jLabel18, jLabel5, jLabel6, jLabel7, jLabel8, jLabel9, mailingaddressField, modifiedbyField, nameField, phonenumberField, telexnumberField, titleField, tradeareaField, uanameField});

    }// </editor-fold>//GEN-END:initComponents

    private void descriptionFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_descriptionFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_descriptionFieldActionPerformed

    private void phonenumberFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_phonenumberFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_phonenumberFieldActionPerformed

    private void datecreatedFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_datecreatedFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_datecreatedFieldActionPerformed

    private void telexnumberFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_telexnumberFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_telexnumberFieldActionPerformed

    private void uanameFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_uanameFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_uanameFieldActionPerformed

    private void titleFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_titleFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_titleFieldActionPerformed

    private void mailingaddressFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mailingaddressFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_mailingaddressFieldActionPerformed

    private void emailaddressFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_emailaddressFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_emailaddressFieldActionPerformed

    private void addUserButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addUserButtonActionPerformed
        resetFields();
    }//GEN-LAST:event_addUserButtonActionPerformed

    private void deleteUserButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteUserButtonActionPerformed
        deleteUser();
    }//GEN-LAST:event_deleteUserButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addUserButton;
    private javax.swing.JTextField createdbyField;
    private javax.swing.JTextField datecreatedField;
    private javax.swing.JTextField datemodifiedField;
    private javax.swing.JButton deleteUserButton;
    private javax.swing.JTextField descriptionField;
    private javax.swing.JTextField emailaddressField;
    private javax.swing.JTextField faxnumberField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JTextField mailingaddressField;
    private javax.swing.JTextField modifiedbyField;
    private javax.swing.JTextField nameField;
    private javax.swing.JTextField phonenumberField;
    private javax.swing.JTextField telexnumberField;
    private javax.swing.JTextField titleField;
    private javax.swing.JTextField tradeareaField;
    private javax.swing.JTextField uanameField;
    private org.shop.editor.UserDeleteActionListener userDeleteActionListener1;
    // End of variables declaration//GEN-END:variables

    private void modify() {
        if (getLookup().lookup(MySavable.class) == null) {
            instanceContent.add(new MySavable());
        }
    }

    @Override
    public UndoRedo getUndoRedo() {
        return manager;
    }

    @Override
    public void componentOpened() {
        //result = Utilities.actionsGlobalContext().lookupResult(EqcUsers.class);
        result = WindowManager.getDefault().findTopComponent("UserViewerTopComponent").getLookup().lookupResult(EqcUsers.class);
        result.addLookupListener(this);
        resultChanged(new LookupEvent(result));
    }

    @Override
    public void componentClosed() {
        result.removeLookupListener(this);
        result = null;
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    void readProperties(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }

    //@Override
    //public void resultChanged(LookupEvent le) {
    //    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    //}
    /**
     *
     * @param lookupEvent
     */
    @Override
    public void resultChanged(LookupEvent lookupEvent) {

        Lookup.Result r = (Lookup.Result) lookupEvent.getSource();
        Collection<EqcUsers> coll = r.allInstances();
        
        if (!coll.isEmpty()) {
            for (EqcUsers users : coll) {
                uanameField.setText(users.getUaName());
                descriptionField.setText(users.getDescription());
                nameField.setText(users.getName());
                titleField.setText(users.getTitle());



                if (users.getPhoneNumber() == null) {
                    phonenumberField.setText("");
                } else {
                    StringBuilder sb = new StringBuilder(15);
                    StringBuilder temp = new StringBuilder(users.getPhoneNumber());

                    while (temp.length() < 10) {
                        temp.insert(0, "0");
                    }
                    char[] chars = temp.toString().toCharArray();
                    sb.append("(");
                    for (int i = 0; i < chars.length; i++) {
                        if (i == 3) {
                            sb.append(") ");
                        } else if (i == 6) {
                            sb.append("-");
                        }
                        sb.append(chars[i]);
                    }
                    System.out.println(sb.toString());
                    phonenumberField.setText(sb.toString());
                }


                //phonenumberField.setText(users.getPhoneNumber());
                faxnumberField.setText(users.getFaxNumber());
                telexnumberField.setText(users.getTelexNumber());

                mailingaddressField.setText(users.getMailAddress());
                emailaddressField.setText(users.getElectronicMailAddress());
                tradeareaField.setText(users.getTradeArea());

                Format formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                //--------------------------------------------
                // convert date created - handle nulls
                String s;
                Date d = users.getDateCreated();
                if (d == null) {
                    s = "";
                } else {
                    s = formatter.format(d);
                }
                datecreatedField.setText(s);
                //-----------------------------------------
                // convert date modified - handle null
                String t;
                Date e = users.getDateModified();
                if (e == null) {
                    t = "";
                } else {
                    t = formatter.format(users.getDateModified());
                }
                datemodifiedField.setText(t);
                //------------------------------------------
                createdbyField.setText(users.getCreatedBy());
                modifiedbyField.setText(users.getModifiedBy());
                addnew = false;
            }
        } else {
            resetFields();
        }
    }
    private static final Icon ICON = ImageUtilities.loadImageIcon("org/shop/editor/Icon.png", true);

    public void resetFields() {
        eqcusers = new EqcUsers();
        uanameField.setText("");
        descriptionField.setText("");
        nameField.setText("");
        titleField.setText("");
        phonenumberField.setText("");
        faxnumberField.setText("");
        telexnumberField.setText("");
        mailingaddressField.setText("");
        emailaddressField.setText("");
        tradeareaField.setText("");
        datecreatedField.setText("");
        createdbyField.setText("");
        datemodifiedField.setText("");
        modifiedbyField.setText("");
        addnew = true;
    }

    private void deleteUser() {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        Confirmation message;
        Message zmessage;

        message = new NotifyDescriptor.Confirmation("Do you want to delete record with this key: \""
                + uanameField.getText() + "\"?",
                "Please confirm",
                NotifyDescriptor.OK_CANCEL_OPTION,
                NotifyDescriptor.QUESTION_MESSAGE);
        Object msgresult = DialogDisplayer.getDefault().notify(message);

        if (NotifyDescriptor.YES_OPTION.equals(msgresult)) {
            // add jpa code for persisting our change
            //EntityManager em = Persistence.createEntityManagerFactory("RussCustomerLibraryPU").createEntityManager();
            EntityManager em = Persistence.createEntityManagerFactory("QnrLibraryPU").createEntityManager();
            em.getTransaction().begin();
            EqcUsers eqcusers = em.find(EqcUsers.class, uanameField.getText());
            em.remove(eqcusers);
            em.getTransaction().commit();

            System.out.println("Deleting uaname = " + uanameField.getText());


            zmessage = new NotifyDescriptor.Message("User successfully deleted: \""
                    + uanameField.getText() + "\" "); //,
            Object okresult = DialogDisplayer.getDefault().notify(zmessage);

            UserViewerTopComponent uvtc = (UserViewerTopComponent) WindowManager.getDefault().findTopComponent("UserViewerTopComponent");
            uvtc.refreshNode();
        }
    }

    //private class MySavable extends AbstractSavable implements Icon {
    public class MySavable extends AbstractSavable implements Icon {    

        MySavable() {
            register();
        }

        @Override
        protected String findDisplayName() {
            String name = uanameField.getText();
            //String city = cityField.getText();
            return name; 
        }

        @Override
        protected void handleSave() throws IOException {
            Confirmation message = new NotifyDescriptor.Confirmation("Do you want to save \""
                    + uanameField.getText() + "\"?",
                    NotifyDescriptor.OK_CANCEL_OPTION,
                    NotifyDescriptor.QUESTION_MESSAGE);
            Object result = DialogDisplayer.getDefault().notify(message);
            if (NotifyDescriptor.YES_OPTION.equals(result)) {
                // add jpa code for persisting our change
                //EntityManager entityManager = Persistence.createEntityManagerFactory("RussCustomerLibraryPU").createEntityManager();
                EntityManager entityManager = Persistence.createEntityManagerFactory("QnrLibraryPU").createEntityManager();
                entityManager.getTransaction().begin();

                if (!addnew) {
                    //*if (eqcusers.getUaName() != null) { */
                    EqcUsers c = entityManager.find(EqcUsers.class, uanameField.getText());
                    //System.out.println("uaname = " + uanameField.getText());
                    c.setUaName(uanameField.getText());
                    c.setDescription(descriptionField.getText());
                    c.setName(nameField.getText());
                    c.setTitle(titleField.getText());
                    c.setPhoneNumber(phonenumberField.getText());
                    c.setFaxNumber(faxnumberField.getText());
                    c.setTelexNumber(telexnumberField.getText());
                    c.setMailAddress(mailingaddressField.getText());
                    c.setElectronicMailAddress(emailaddressField.getText());
                    c.setTradeArea(tradeareaField.getText());
                    entityManager.getTransaction().commit();
                    addnew = false;

                } else {
                    Query query = entityManager.createNamedQuery("EqcUsers.findAll");
                    List<EqcUsers> resultList = query.getResultList();
                    eqcusers.setUaName(uanameField.getText());
                    eqcusers.setDescription(descriptionField.getText());
                    eqcusers.setName(nameField.getText()); //)xtradeareaField        
                    eqcusers.setTitle(titleField.getText());
                    eqcusers.setPhoneNumber(phonenumberField.getText());
                    eqcusers.setFaxNumber(faxnumberField.getText());
                    eqcusers.setTelexNumber(telexnumberField.getText());
                    eqcusers.setMailAddress(mailingaddressField.getText());
                    eqcusers.setElectronicMailAddress(emailaddressField.getText());
                    eqcusers.setTradeArea(tradeareaField.getText());
                    entityManager.persist(eqcusers);
                    entityManager.getTransaction().commit();
                    addnew = false;
                }
                tc().instanceContent.remove(this);
                unregister();
                // refresh the viewer
                UserViewerTopComponent uvtc = (UserViewerTopComponent) WindowManager.getDefault().findTopComponent("UserViewerTopComponent");
                uvtc.refreshNode();
            }
        }

        UserEditorTopComponent tc() {
            return UserEditorTopComponent.this;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj instanceof MySavable) {
                MySavable m = (MySavable) obj;
                return tc() == m.tc();
            }
            return false;
        }

        @Override
        public int hashCode() {
            return tc().hashCode();
        }

        @Override
        public void paintIcon(Component c, Graphics g, int x, int y) {
            ICON.paintIcon(c, g, x, y);
        }

        @Override
        public int getIconWidth() {
            return ICON.getIconWidth();
        }

        @Override
        public int getIconHeight() {
            return ICON.getIconHeight();
        }
    }
}
